package cn.kaiux.bigdata.homework.week02;

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.util.Bytes;

/**
 * @author kaiux@foxmail.com
 * @date 2022/5/9 15:59
 */
@Slf4j
public class HBaseTest {

    public static void main(String[] args) {

        String namespace = "yulikai";
        String tableName = namespace + ":student";

        //创建namespace
        HBaseUtils.createNamespace(namespace);

        //创建表
        HBaseUtils.createTable(tableName, Lists.newArrayList("name", "info", "score"));

        // TOM
        HBaseUtils.putRow(tableName, "20210000000001_Tom", "name", "name", "Tom");
        HBaseUtils.putRow(tableName, "20210000000001_Tom", "info", "student_id", "20210000000001");
        HBaseUtils.putRow(tableName, "20210000000001_Tom", "info", "class", "1");
        HBaseUtils.putRow(tableName, "20210000000001_Tom", "score", "understanding", "75");
        HBaseUtils.putRow(tableName, "20210000000001_Tom", "score", "programming", "82");
        // Jerry
        HBaseUtils.putRow(tableName, "20210000000002_Jerry", "name", "name", "Jerry");
        HBaseUtils.putRow(tableName, "20210000000002_Jerry", "info", "student_id", "20210000000002");
        HBaseUtils.putRow(tableName, "20210000000002_Jerry", "info", "class", "1");
        HBaseUtils.putRow(tableName, "20210000000002_Jerry", "score", "understanding", "85");
        HBaseUtils.putRow(tableName, "20210000000002_Jerry", "score", "programming", "67");
        // Jack
        HBaseUtils.putRow(tableName, "20210000000003_Jack", "name", "name", "Jack");
        HBaseUtils.putRow(tableName, "20210000000003_Jack", "info", "student_id", "20210000000003");
        HBaseUtils.putRow(tableName, "20210000000003_Jack", "info", "class", "2");
        HBaseUtils.putRow(tableName, "20210000000003_Jack", "score", "understanding", "80");
        HBaseUtils.putRow(tableName, "20210000000003_Jack", "score", "programming", "80");
        // Rose
        HBaseUtils.putRow(tableName, "20210000000004_Rose", "name", "name", "Rose");
        HBaseUtils.putRow(tableName, "20210000000004_Rose", "info", "student_id", "20210000000004");
        HBaseUtils.putRow(tableName, "20210000000004_Rose", "info", "class", "2");
        HBaseUtils.putRow(tableName, "20210000000004_Rose", "score", "understanding", "60");
        HBaseUtils.putRow(tableName, "20210000000004_Rose", "score", "programming", "61");
        // yulikai
        HBaseUtils.putRow(tableName, "G20220735030005_yulikai", "name", "name", "yulikai");
        HBaseUtils.putRow(tableName, "G20220735030005_yulikai", "info", "student_id", "G20220735030005");
        HBaseUtils.putRow(tableName, "G20220735030005_yulikai", "info", "class", "2");
        HBaseUtils.putRow(tableName, "G20220735030005_yulikai", "score", "understanding", "98");
        HBaseUtils.putRow(tableName, "G20220735030005_yulikai", "score", "programming", "99");

        // 扫描表数据
        ResultScanner resultScanner = HBaseUtils.getScanner(tableName);
        StringBuilder resultBuilder = new StringBuilder();
        if (null == resultScanner) {
            log.warn("get resultScanner from table:[{}] is null", tableName);
            return;
        }
        for (Result result : resultScanner) {
            for (Cell cell : result.rawCells()) {
                String colName = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
                String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
                resultBuilder.append(colName).append(":").append(value).append("\t");
            }
            resultBuilder.append("\n");
        }
        log.info("scan table:[{}] get result:\n{}", tableName, resultBuilder);
    }
}